package com.ttt.gdfood.controller;

import com.ttt.gdfood.service.ex.*;
import com.ttt.gdfood.util.JsonResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;

// 控制器的基类
public class BaseController {
    // 操作成功的状态码
    public static final int OK = 200;
    // @ExceptionHandler用于统一处理方法抛出的异常
    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> result = new JsonResult<Void>(e);
        if (e instanceof UsernameDuplicateException) {
            result.setState(400);
            result.setMessage("用户已存在");
        } else if (e instanceof InsertException) {
            result.setState(500);
        } else if (e instanceof UserNotFoundException) {
            result.setState(401);
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(402);
        }
        return result;
    }

}

